package com.naire.modul.kpi.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.kpi.dao.KpiDao;
import com.naire.modul.kpi.pojo.Kpi;
import com.naire.modul.kpi.vo.KpiVo;

public class KpiDaoImpl extends GeneralDaoImpl implements KpiDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<KpiVo> search(PageBean pageBean, String kpi_num, String dict_num) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("from Kpi where flag=0");
		if(dict_num != null && !dict_num.equals("")) {
			hql.append(" and catalogue_num=:catalogue_num");
			params.put("catalogue_num", dict_num);
		} 
		if(kpi_num != null && !kpi_num.equals("")) {
			hql.append(" and parent_num=:parent_num");
			params.put("parent_num", kpi_num);
		} else {
			hql.append(" and parent_num is null");
		}
		List<KpiVo> d = new ArrayList<KpiVo>();
		List<Kpi> l = hibernateHandler.query(hql, pageBean, params);
		for(Kpi bean : l) {
			KpiVo dv = new KpiVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(KpiVo bean) throws Exception {
		Kpi pojo = new Kpi();
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(KpiVo bean) throws Exception {
		Kpi pojo = (Kpi)hibernateHandler.find(Kpi.class, bean.getKpi_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(KpiVo bean) throws Exception {
		update(bean);
	}

	@Override
	public KpiVo find(Integer id) throws Exception {
		Kpi pojo = (Kpi)hibernateHandler.find(Kpi.class, id);
		KpiVo bean = new KpiVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}
}
